<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/mapm' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <el-card class="notice">
      <h4>&nbsp;&nbsp;用户列表</h4>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.txt" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getUser"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--用户列表区-->
      <el-table :data="user" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column label="姓名" prop="username"> </el-table-column>
        <el-table-column label="身份证号" prop="idNumber"> </el-table-column>
        <el-table-column label="性别" prop="gender">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.gender === 1">
              女
            </el-tag>
            <el-tag v-if="scope.row.gender === 0">
              男
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="telephone"> </el-table-column>
        <el-table-column label="操作" width="270px">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="removeMaterialByName(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'UserManage',
  data () {
    return {
      // 获取用户列表的参数
      queryInfo: {
        // 当前页码
        pageNum: 1,
        pageSize: 5,
        txt: ''
      },
      total: 0,
      user: [],
      name: ''
    }
  },
  created () {
    this.getUser()
  },
  methods: {
    getUser () {
      this.$axios.get('searchbypage/commonuser', {
        params: this.queryInfo
      }).then(res => {
        const info = res.data
        if (info.code === 401) {
          console.log(info)
          this.$message({
            message: '获取文件数据失败',
            type: 'error',
            offset: 70
          })
        }
        this.user = info.data.list
        this.total = info.data.total
      }).catch(err => {
        console.log(err)
      })
    },
    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getUser()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getUser()
    },
    async removeMaterialByName (id) {
      // 弹框询问
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      // 如果用户确认删除，则返回值为字符串confirm
      // 如果用户确认删除，则返回值为字符串cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$axios.delete('admin/user/' + id)
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      this.getUser()
    }
  }
}
</script>

<style scoped>
.navigation {
  padding: 10px;
}

.notice {
  margin-bottom: 40px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}

h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}

.el-table {
  margin-top: 15px;
}

/* 页码 */
.el-pagination {
  margin-top: 15px;
}
</style>
